package com.FengChaoSystem.service.OrderService;

import com.FengChaoSystem.dao.OrderDao;
import com.FengChaoSystem.pojo.Occupy;
import com.FengChaoSystem.pojo.Order;
import com.FengChaoSystem.pojo.PageNeed;
import com.FengChaoSystem.pojo.User;
import com.FengChaoSystem.service.OrderImpl;
import com.FengChaoSystem.untils.SqlSessionFactoryUtils;
import com.Mybatis.session.SqlSession;
import com.Mybatis.session.SqlSessionFactory;

import java.util.List;

public class OrderService implements OrderImpl {
    SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public List<Order> selectAllOrder(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllOrder(user);
        sqlSession.close();
        return orders;
    }

    @Override
    public List<Order> selectAllUnGetOrder(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllUnGetOrder(user);
        sqlSession.close();
        return orders;
    }

    @Override
    public List<Order> selectAllUnGetOrderByCondition(PageNeed pageNeed) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> rows = mapper.selectAllUnGetOrderByCondition(pageNeed);
        sqlSession.close();
        return rows;
    }

    @Override
    public List<Order> selectAllOrderByCondition(PageNeed pageNeed) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllOrderByCondition(pageNeed);
        sqlSession.close();
        return orders;
    }

    @Override
    public Order selectGetWordAndReceiveAddress(Order target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        Order result = mapper.selectGetWordAndReceiveAddress(target);
        sqlSession.close();
        return result;
    }

    @Override
    public Order selectOrdersByOnlyId(Order target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        Order result = mapper.selectOrdersByOnlyId(target);
        sqlSession.close();
        return result;
    }

    @Override
    public List<Order> selectAllSendingOrder(User target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllSendingOrder(target);
        sqlSession.close();
        return orders;
    }

    @Override
    public List<Order> selectAllUnSendOrder(User target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllUnSendOrder(target);
        sqlSession.close();
        return orders;
    }

    @Override
    public List<Order> selectAllUnSendOrderByCondition(PageNeed pageNeed) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllUnSendOrderByCondition(pageNeed);
        sqlSession.close();
        return orders;
    }

    @Override
    public List<Order> selectAllSendingOrderByCondition(PageNeed pageNeed) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        List<Order> orders = mapper.selectAllSendingOrderByCondition(pageNeed);
        sqlSession.close();
        return orders;
    }

    @Override
    public void updateStatus(Order order) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        mapper.updateStatus(order);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Occupy hasOrder(Occupy occupy) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        Occupy order = mapper.hasOrder(occupy);
        sqlSession.close();
        return order;
    }

    @Override
    public Order select(Order target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        Order result = mapper.select(target);
        sqlSession.close();
        return result;
    }

    @Override
    public void updateGetTime(Order target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        mapper.updateGetTime(target);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updatePutTime(Order target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderDao mapper = sqlSession.getMapper(OrderDao.class);
        mapper.updatePutTime(target);
        sqlSession.commit();
        sqlSession.close();
    }


}
